package com.chc.web.borrow.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chc.config.jwt.JwtUtils;
import com.chc.utils.CommonResult;
import com.chc.web.book.entity.Book;
import com.chc.web.borrow.entity.Borrow;
import com.chc.web.borrow.entity.BorrowParm;
import com.chc.web.borrow.service.BorrowService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created with IntelliJ IDEA
 *
 * @Author: chc
 * @Date: 2022/04/22/17:42
 * @Description:
 */

@RestController
@RequestMapping("/api/borrow")
public class BorrowController {
    @Autowired
    private BorrowService borrowService;

    @Autowired
    private JwtUtils jwtUtils;

    //保存读者选择的图书
    @PostMapping
    public CommonResult addBorrow(@RequestBody List<Borrow> borrow){

        int i = borrowService.addBorrow(borrow);
        if(i>0){
            return CommonResult.success("保存图书成功");
        }
        return CommonResult.error("保存图书失败");
    }


    //获取读者选择的图书
    @GetMapping("/getBorrowLook")
    public CommonResult getBorrowLook(BorrowParm parm, HttpServletRequest request){
        //从头部获取token
        String token = request.getHeader("X-Token");
        Claims claims = jwtUtils.getClaimsFromToken(token);
        //获取用户类型
        String userType = (String) claims.get("userType");
        if (userType.equals("0")){ //读者
            IPage<BorrowParm> borrowLook = borrowService.getBorrowLook(parm);
            return CommonResult.success("查询成功",borrowLook);
        }else{
            Long id = null;
            parm.setId(id);
            IPage<BorrowParm> borrowLook = borrowService.getBorrowLook(parm);
            return CommonResult.success("查询成功",borrowLook);
        }

    }

    //获取读者选择的图书
    @GetMapping("/getReturnBook")
    public CommonResult getReturnBook(BorrowParm parm){
        IPage<BorrowParm> borrowLook = borrowService.getReturnBook(parm);
        return CommonResult.success("查询成功",borrowLook);
    }

    //审核通过
    @DeleteMapping("/{id}")
    public CommonResult PassBorrow(@PathVariable("id") Long id){
        Borrow borrow = new Borrow();
        //当前要审核的借阅id
        borrow.setId(id);
        //1为审核通过
        borrow.setState(1L);
        boolean save = borrowService.updateById(borrow);
        if(save){
            return CommonResult.success("审核通过");
        }
        return CommonResult.error("审核失败");
    }

    //归还图书
    @RequestMapping(value = "returnBorrow/{id}", method = RequestMethod.DELETE)
    public CommonResult ReturnBorrow(@PathVariable("id") Long id){
        Borrow borrow = new Borrow();
        //当前要审核的借阅id
        borrow.setId(id);
        //1为审核通过
        borrow.setState(2L);
        boolean save = borrowService.updateById(borrow);
        if(save){
            return CommonResult.success("审核通过");
        }
        return CommonResult.error("审核失败");
    }

}
